from game import zz_game
from zz import zz_log, zz_img, zz_code
import uiautomator2 as u2

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "塔防精灵"

    def game_alert(self):
        self.alert_close_by_file("./game/img/塔防精灵")

    def check_done(self):
        return False

    def handle_main(self):
        self.log_msg("等待进入游戏开始界面")
        self.img_exit("./game/img/塔防精灵/game_start_main.png", 100)
        self.log_msg("已进入游戏开始界面")

        self.log_msg("点击开始游戏")
        self.img_click("./game/img/塔防精灵/game_start.png")

        self.log_msg("等待进入游戏主界面")
        self.img_exit("./game/img/塔防精灵/game_main.png", 100)
        self.log_msg("已进入游戏主界面")

        for i in range(3):
            self.log_msg(f"开始第{i + 1}场战斗")
            self.game_alert()

            self.img_click_if_exists('点击开始战斗', "./game/img/塔防精灵/play.png", "./game/img/塔防精灵/play_01.png")
            self.log_msg(f"点击快速匹配")
            self.img_click("./game/img/塔防精灵/play_01.png")
            # self.img_click_if_exists('点击快速匹配', "./game/img/塔防精灵/play_01.png", "./game/img/塔防精灵/play_match.png")
            self.log_msg("等待进入战斗界面")
            self.img_exit("./game/img/塔防精灵/play_main.png", 100)
            self.log_msg("已进入战斗界面")
            self.d.sleep(2)
            self.img_click_if_exists('点击退出', "./game/img/塔防精灵/play_back.png", "./game/img/塔防精灵/play_02.png")
            self.log_msg(f"点击确定")
            self.img_click("./game/img/塔防精灵/play_02.png")
            self.log_msg("等待战斗失败界面")
            self.img_exit("./game/img/塔防精灵/play_fail.png", 100)
            self.log_msg(f"点击确定")
            self.img_click("./game/img/塔防精灵/play_03.png")

        return True
